package com.lagou.Title4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Myserver {
    //运行服务器的静态代码
    public static void run() {

        //定义变量名
        ServerSocket serverSocket = null;
        Socket socket = null;
        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objectOutputStream = null;
        UserMessage userMessage = null;
        try {
            //创建ServerSocket类型的对象并提供端口号
            serverSocket = new ServerSocket(8888);

            System.out.println("等待服务器连接。。。");

            while (true) {
                //当没有客户端连接时，则服务器阻塞在accept方法的调用处
                socket = serverSocket.accept();
                //输出成功连接的客户端地址
                System.out.println("客户端" + socket.getInetAddress() + "连接成功！");

                //初始化输入输出流
                objectInputStream = new ObjectInputStream(socket.getInputStream());
                objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                try {
                    //读取客户端发送的用户信息
                    userMessage = (UserMessage) objectInputStream.readObject();
                    System.out.println("读取客户端发送的信息为：" + userMessage);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

                //判断用户信息是否正确，并发送验证结果给客户端
                if (userMessage != null && "check".equals(userMessage.getType())) {
                    User user = userMessage.getUser();
                    if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
                        objectOutputStream.writeObject(new UserMessage("success", user));
                        System.out.println("发送 success 信息给客户端");
                    } else {
                        objectOutputStream.writeObject(new UserMessage("fail", user));
                        System.out.println("发送fail信息给客户端");
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("服务器下线");
            //关闭流并释放有关的资源
            if (null != objectOutputStream) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != objectInputStream) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != serverSocket) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        run();
    }
}

